//
//  LGKNoteWebView.m
//  LGKNote
//
//  Created by 刘亚军 on 2023/5/30.
//

#import "LGKNoteWebView.h"
#import <objc/runtime.h>
@interface _NoInputAccessoryView : NSObject
@end
@implementation _NoInputAccessoryView
- (id)inputAccessoryView {
 return nil;
}
@end

@interface LGKNoteWebView ()
@end

@implementation LGKNoteWebView
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{

    if (action == @selector(select:) ||
        action == @selector(selectAll:) ||
        action == @selector(paste:) ||
        action == @selector(_lookup:)){
        return YES;
    }
    return NO;
}

// 去掉 WkWebviewe Done 工具栏
- (void)hideWKWebviewKeyboardShortcutBar {
    UIView *targetView;
    WKWebView *webView = self;
    for (UIView *view in webView.scrollView.subviews) {
        if([[view.class description] hasPrefix:@"WKContent"]) {
            targetView = view;
        }
    }
    if (!targetView) {
        return;
    }
    NSString *noInputAccessoryViewClassName = [NSString stringWithFormat:@"%@_NoInputAccessoryView", targetView.class.superclass];
    Class newClass = NSClassFromString(noInputAccessoryViewClassName);
    
    if(newClass == nil) {
        newClass = objc_allocateClassPair(targetView.class, [noInputAccessoryViewClassName cStringUsingEncoding:NSASCIIStringEncoding], 0);
        if(!newClass) {
            return;
        }
        
        Method method = class_getInstanceMethod([_NoInputAccessoryView class], @selector(inputAccessoryView));
        
        class_addMethod(newClass, @selector(inputAccessoryView), method_getImplementation(method), method_getTypeEncoding(method));
        
        objc_registerClassPair(newClass);
    }
    
    object_setClass(targetView, newClass);
}
@end
